//
//  LLMyPublishListViewController.m
//  LoveLove
//
//  Created by 达迩科技 on 2019/4/10.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLMyPublishListViewController.h"
#import "LLboughtContentTableViewCell.h"
#import "LLMyCourseTableViewCell.h"
#import "LLCourseEditView.h"
#import "LLMyPublishModel.h"
#import "LLPublishCell.h"

#import "LLNewsVideoDetialVC.h"
#import "LLNewsImageDetialVC.h"
#import "QWebVC.h"

@interface LLMyPublishListViewController ()<LLMyCourseTableViewCellDelete,LLCourseEditViewDelete,UINavigationControllerDelegate>
@property (strong, nonatomic) LLCourseEditView *courseEditView;
@property (nonatomic,strong) UILabel *lab;
@end

@implementation LLMyPublishListViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [SVProgressHUD dismiss];
    self.titleView.backgroundColor = LLColor(227, 15, 29);
    self.titleLabel.text = @"我发布的";
//    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
//       [self sendMyPublishRequst];
//    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
      
        [self.models removeAllObjects];
       [self sendMyPublishRequst];
    }];
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark - 显示视图操作方法
- (void)configTableView {
    self.needRefresh = NO;
    [super configTableView];
    self.tableView.backgroundColor = LLColorRGB(243);
//    self.tableView.mj_h = LLScreenHeight;
    [self.tableView registerCellByClassName:@"LLboughtContentTableViewCell"];
    [self.tableView registerCellByClassName:@"LLMyCourseTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(-LLNavH, 0,LLNavH, 0);
    self.tableView.frame = CGRectMake(0, 0, LLScreenWidth, LLScreenHeight);
    [self addHeadView];
}
-(void)addHeadView{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, 40)];
    self.lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, LLScreenWidth, 40)];
    self.lab.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:self.lab ];
    self.tableView.tableHeaderView = headView;
}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.type == 0) {
//        LLMyCourseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLMyCourseTableViewCell"];
//        cell.accessoryType = UITableViewCellAccessoryNone;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;//无高亮选中
//        cell.backgroundColor = [UIColor whiteColor];
//        cell.delegate = self;
//        cell.backgroundColor = LLColorRGB(243);
//        return cell;
//    } else {
//        LLboughtContentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLboughtContentTableViewCell"];
//        cell.accessoryType = UITableViewCellAccessoryNone;
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;//无高亮选中
//        cell.backgroundColor = [UIColor whiteColor];
//        return cell;
//    }
    
    LLPublishCell *cell = [LLPublishCell publishCellWithTableView:tableView andIndexPath:indexPath];
    [cell configPublishCell:self.models[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.type == 0) {
        return 120;
    } else {
        return 90;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    LLMyPublishModel *model = self.models[indexPath.row];

    LLStatusModel *new_model = [LLStatusModel new];
    new_model.newsId = model.newsId;
    new_model.u_name = model.newsTitle;

    if ([model.type intValue] == 1) {

        LLNewsVideoDetialVC *vc = [LLNewsVideoDetialVC new];
        vc.model = new_model;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else if ([model.type intValue] == 2 ||
             [model.type intValue] == 3 ||
             [model.type intValue] == 4) {

        LLNewsImageDetialVC *vc = [LLNewsImageDetialVC new];
        vc.model = new_model;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)didSelectButton:(LLMyCourseTableViewCell *)cell didClickItem:(NSInteger)item{
    //编辑
    if (item == 0) {
        
    }
    //更多
    else {
        [self courseEditView];
        self.courseEditView.hidden = NO;
    }
}

- (void)didSelectButtonCaouzuo:(LLCourseEditView *)view didClickItem:(NSInteger)item{
    //分享
    if (item == 1) {
        
    }
    //下架
    else if (item == 2) {
        
    }
    //删除
    else if (item == 3) {
        
    }
    _courseEditView.hidden = YES;
}


-(LLCourseEditView *)courseEditView{
    if (_courseEditView == nil) {
        _courseEditView = [[LLCourseEditView alloc] initWithFrame:CGRectMake(0, 0, LLScreenWidth, LLScreenHeight)];
        _courseEditView.hidden = YES;
        _courseEditView.delegate = self;
        [[UIApplication sharedApplication].keyWindow addSubview:_courseEditView];
    }
    return _courseEditView;
}


#pragma mark ---- dealloc
- (void)dealloc {
    if (_courseEditView) {
        [_courseEditView removeFromSuperview];
    }
}


#pragma mark---我的发布
-(void)sendMyPublishRequst{
    LLUserInfo *user = [LLUserInfo sharedUser];
    
    NSDictionary *dic = @{@"edu_id":@"24",@"user_random":user.user_random,@"user_id":user.user_id};
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.page++;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSArray *dataArr = [NSArray yy_modelArrayWithClass:[LLMyPublishModel class] json:result[@"data"]];
        
        self.lab.text = [NSString stringWithFormat:@"一共发布了%ld篇文章",dataArr.count];
        if (dataArr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        
        [self.models addObjectsFromArray:dataArr];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
